import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-26
 * Time: 9:37
 */
//使用链表模拟实现栈
class Node{
    public int value;
    public Node prev; //存放前一个结点的地址
    public Node next; //存放后一个结点的地址
    public Node(int value){
        this.value = value;
    }
}

public class MyLinkStack {
    public Node head; //指向头结点
    public Node last; //指向尾巴结点

    //入栈（使用头插法）
    public void push(int value){
        Node node = new Node(value);
        if(head==null){
            this.head = node;
            this.last = node;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    //出栈 (删除头结点)
    public int pop(){
        if(isEmpty()){
            throw new NullPointerException("栈为空！");
        }
        int oldValue = head.value;
        if(head==last) {
            //清空链表
            head.next= null;
        }
        head = head.next;
        return oldValue;
    }

    //读取栈顶元素
    public int peek(){
        if(isEmpty()){
            throw new NullPointerException("栈为空！");
        }
        return this.head.value;
    }

    //判断栈是否为空
    public boolean isEmpty(){
        return this.head==null;
    }
}
